const { Service } = require('egg')
const { tableToList } = require('../utils/tools')

module.exports = class SinfoService extends Service {
  /**
   * * 学籍卡片信息
   * @return {object} *
   */
  async table() {
    const Cookie = this.ctx.state.Cookie
    const apiUrl = `${this.ctx.state.baseUrl}/eams/stdDetail.action?_=1717433272038`
    // console.log('apiUrl', apiUrl)
    const res = await this.ctx.curl(apiUrl, {
      method: 'GET',
      headers: {
        Cookie,
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      timeout: 10000,
    })
    console.log('res', res)
    const info = {}

    if (res.status === 200) {
      try {
        const tableList = tableToList(res.data.toString(), '#tabPage1')
        console.log('tableList', tableList)
        // /* 处理院系、专业*/
        let key = ''
        tableList[5].filter((e) => e).slice(0, 4).forEach((e, i) => {
          if (i % 2 === 0) {
            info[e] = ''
            key = e
          }
          else info[key] = e
        })

        // /* 处理年级、学制 */
        tableList[3].filter((e) => e).slice(0, 4).forEach((e, i) => {
          if (i % 2 === 0) {
            info[e] = ''
            key = e
          }
          else info[key] = e
        })

        // /* 处理学号、姓名 */
        tableList[1].filter((e) => e).slice(0, 4).forEach((e, i) => {
          if (i % 2 === 0) {
            info[e] = ''
            key = e
          }
          else info[key] = e
        })
        // /* 处理专业 */
        tableList[6].filter((e) => e).slice(0, 2).forEach((e, i) => {
          if (i % 2 === 0) {
            info[e] = ''
            key = e
          }
          else info[key] = e
        })
        // /* 处理专业 */
        tableList[11].filter((e) => e).slice(0, 4).forEach((e, i) => {
          if (i % 2 === 0) {
            info[e] = ''
            key = e
          }
          else info[key] = e
        })

        // info['年级'] = info['学号'].slice(1, 5)

        return info
      } catch (err) {
        console.log('err', err)
        throw err
      }
    }
    return info
  }
}
